/**
 * 工程名: 	Quickdisk
 * 文件名: 	UserDao.java
 * 创建人:  	guo.tianxiang
 * 创建时间: 	2014-4-24 下午4:37:50
 * 版权所有：	Guo tianxiang  
 * 文件描述: 描述该文件的作用
 * -----------------------------变更记录 ----------------------------- 
 * 日期        		变更人      		版本号  		变更描述  
 * ------------------------------------------------------------------  
 * 2014-4-24     guo.tianxiang   	1.0       	first created  
 */
package com.tianxiang.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.tianxiang.hibernatesession.HibernateSessionFactory;
import com.tianxiang.model.User;

/**
 * 进行数据库操作的类
 * 
 * @since Quickdisk 应用程序版本号
 * @version 1.0 2014-4-24
 * @author guo.tianxiang
 */
public class UserDao {
	Session getSession() {// 获取session
		return HibernateSessionFactory.getSession();
	}

	@SuppressWarnings("unchecked")
	public List<User> findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from User as s where s." + propertyName
					+ "=?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (HibernateException e) {
			throw e;
		}
	}
	/**
	 * 根据id获取用户实例
	 * @param userId
	 * @return
	 */
	public User findUserById(int userId){
		Query queryObject = getSession().createQuery("from User as s where s.id=?");
		queryObject.setParameter(0, userId);
		@SuppressWarnings("unchecked")
		List<User> list=queryObject.list();
		for (User user : list) {
			return user;
		}
		return null;
	}
	
/**
 * 根据用户名获取指定字段值
 * @param username 用户名
 * @param property 指定字段
 * @return
 */
	public Object getPropertyById(int id, String property) {
		String querySql = "from User as s where s.id=?";
		Query queryObject = getSession().createQuery(querySql);
		queryObject.setParameter(0, id);
		@SuppressWarnings("unchecked")
		List<User> resultList = queryObject.list();
		for (User user : resultList) {
			if (property.equals("restsize")) {
				return user.getTotalspace() - user.getUsedspace();
			} else if (property.equals("nickname")) {
				return user.getNickname();
			} else if (property.equals("totalspace")) {
				return user.getTotalspace();
			} else if (property.equals("usedspace")) {
				return user.getUsedspace();
			} else if (property.equals("introduce")) {
				return user.getIntroduce();
			} else if (property.equals("sex")) {
				return user.getSex();
			}
		}
		return null;
	}

	/**
	 * 保存对象 描述方法的功能
	 * 
	 * @param user
	 */
	public boolean save(User user) {
		try {
			Transaction transaction = getSession().beginTransaction();
			transaction.begin();// 事务开始
			getSession().saveOrUpdate(user);
			transaction.commit();// 提交事务
		} catch (RuntimeException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 更新user
	 * @param user
	 * @return
	 */
	public boolean update(User user) {
		try {
			Transaction transaction = getSession().beginTransaction();
			transaction.begin();// 事务开始
			getSession().update(user);
			transaction.commit();// 提交事务
		} catch (HibernateException e) {
			return false;
		}
		return true;
	}
	
	public boolean delUser(int id){
		Transaction transaction=getSession().beginTransaction();
		transaction.begin();
		String sqlString="delete User where id="+id;
		Query query=getSession().createQuery(sqlString);
		query.executeUpdate();
		transaction.commit();
		return true;
	}
	/**
	 * 获取全部用户
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<User>getAllUsers () {
		String sqlString="from User";
		Query query=getSession().createQuery(sqlString);
		return query.list();
	}
}